package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.font.JobTo;
import com.jeff.tianti.cms.entity.Resume;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeff.tianti.cms.entity.Job;
import com.jeff.tianti.cms.dto.JobQueryDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;

/**
 * @author liuyi
 * @desc JobDaoImpl类
 * @date 2019-06-28
 */

public class JobDaoImpl extends CustomBaseSqlDaoImpl implements JobDaoCustom {

    public PageModel<Job> queryJobPage(JobQueryDTO jobQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" select t from Job t where 1=1  ");
        if (jobQueryDTO != null) {
            String jobName = jobQueryDTO.getJobName();
            if (StringUtils.isNotBlank(jobName)) {
                hql.append(" and t.jobName like :jobName ");
                map.put("jobName", "%" + jobName + "%");
            }
            String auditFlag = jobQueryDTO.getAuditFlag();
            if (StringUtils.isNotBlank(auditFlag) && !"-1".equals(auditFlag)) {
                hql.append(" and t.auditFlag = :auditFlag ");
                map.put("auditFlag", auditFlag);
            }
            String publishType = jobQueryDTO.getPublishType();
            if (StringUtils.isNoneBlank(publishType) && !"-1".equals(publishType)) {
                hql.append(" and t.publishType = :publishType ");
                map.put("publishType", publishType);
            }
            String sortNum = jobQueryDTO.getSortNum();
            if (StringUtils.isNoneBlank(sortNum) && !"-1".equals(sortNum)) {
                if ("desc".equals(sortNum)) {
                    hql.append(" order by t.createDate desc ");
                } else if ("asc".equals(sortNum)) {
                    hql.append(" order by t.createDate asc ");
                }
            }
            String jobType = jobQueryDTO.getJobType();
            if (StringUtils.isNotBlank(jobType) && !"-1".equals(jobType)) {
                hql.append(" and t.jobType = :jobType ");
                map.put("jobType", jobType);
            }
            String areaId = jobQueryDTO.getAreaId();
            if (StringUtils.isNotBlank(areaId) && !"-1".equals(areaId)) {
                hql.append(" and t.areaId = :areaId ");
                map.put("areaId", areaId);
            }
        }

        return this.queryForPageWithParams(hql.toString(), map, jobQueryDTO.getCurrentPage(), jobQueryDTO.getPageSize());
    }

    public List<Job> queryJobList(JobQueryDTO jobQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Job t ");
        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public PageModel<Resume> queryJobPages(JobTo jobTo) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" select t from Job t where 1=1  ");
        if (jobTo != null) {
            String jobName = jobTo.getJobName();
            if (StringUtils.isNotBlank(jobName)) {
                hql.append(" and t.jobName like :jobName ");
                map.put("jobName", "%" + jobName + "%");
            }
            String publishType = jobTo.getPublishType();
            if (StringUtils.isNotBlank(publishType)) {
                hql.append(" and t.publishType = :publishType ");
                map.put("publishType", publishType);
            }
            String state = jobTo.getState();////0->招聘中 1->停止招聘  2->审核中
//            deleteFlag   auditFlag
            if (StringUtils.isNotBlank(state)) {
                if ("0".equals(state)) {
                    hql.append(" and t.deleteFlag = 0 and t.auditFlag = 2 ");
                }
                if ("1".equals(state)) {
                    hql.append(" and t.deleteFlag = 1 ");
                }
                if ("2".equals(state)) {
                    hql.append(" and t.auditFlag = 0 and t.deleteFlag = 0 ");
                }
            }
            String uid = jobTo.getUid();
            if (StringUtils.isNotBlank(uid)) {
                hql.append(" and t.uid = :uid ");
                map.put("uid", uid);
            }
        }
        hql.append(" order by t.updateDate desc ");
        return this.queryForPageWithParams(hql.toString(), map, jobTo.getCurrentPage() + 1, jobTo.getPageSize());
    }


}